//
//  ViewController.m
//  NSOperationTest
//
//  Created by wangrui on 13-8-23.
//  Copyright (c) 2013年 PPS. All rights reserved.
//

#import "ViewController.h"
#import "MyOperation.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)dosomething:(id)sender{
    NSLog(@"dosomething:%@",sender);
    NSLog(@"%@,%d",[NSThread currentThread],[NSThread isMainThread]);
    
//    while (true) {
//        [[NSRunLoop currentRunLoop] run];
//    }
//    
//    NSLog(@"runloop over");
    
    for (int i=0; i<1000000000; i++) {
        
    }
    
    NSLog(@"over");
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    [self testBasic];
    //[self testInvocationOperation];
    //[self testBlockInvocation];
}

- (void)testBlockInvocation{
    NSBlockOperation *b = [NSBlockOperation blockOperationWithBlock:^{
        [self dosomething:nil];
    }];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue addOperation:b];
}

- (void)testInvocationOperation{
    NSInvocationOperation *o = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(dosomething:) object:nil];
    
//  test1
//
//    [o start];
 
//  test2
//
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue addOperation:o];
    
    NSLog(@"isConcurrent:%d",[o isConcurrent]);
}

- (void)testBasic{
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    for (int i=0; i<10; i++) {
        MyOperation *oper = [[MyOperation alloc]init];
        oper.index = i;
        [queue addOperation:oper];
        //[oper start];
    }
    
    //[queue cancelAllOperations];
}

@end
